package com.wyl.spider.pipline;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.wyl.model.Detail;
import com.wyl.model.Simple;
import com.wyl.service.DetailService;
import com.wyl.service.SimpleService;
import com.wyl.utils.Utils;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

@Component
public class DetailPipeline implements Pipeline{

	@Autowired
	private DetailService detailService;
	@Autowired
	private SimpleService simpleService;
	
	@Override
	@Transactional
	public void process(ResultItems resultItems, Task task) {
		Detail detail = new Detail();
		detail.setTitle(resultItems.get("title").toString());
		detail.setFrom(resultItems.get("from").toString());
		Date time= Utils.parseTime(resultItems.get("time").toString(), "yyyy-dd-MM HH:mm:ss");
		
		detail.setTime(time);
		List<String> pList = resultItems.get("content");
		StringBuilder sb = new StringBuilder();
		for (String string : pList) {
			sb.append(string);
		}
		detail.setContent(sb.toString());
		detail.setOldTitle(resultItems.get("oldTitle").toString());
		Simple simple = simpleService.selectByDocurl(resultItems.get("url").toString());
		simple.setHasContent(true);
		simpleService.setContentState(simple.getId(),true);
		detail.setSimpleId(simple.getId());
		detailService.save(detail);
		
	}

}
